package com.yc.cloudvideo.sp_store.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.cloudvideo.entity.Store;
import com.yc.cloudvideo.entity.StoreFile;
import com.yc.cloudvideo.sp_store.mapper.FileMapper;
import com.yc.cloudvideo.sp_store.mapper.StoreFileMapper;
import com.yc.cloudvideo.sp_store.mapper.StoreMapper;
import com.yc.cloudvideo.sp_store.service.impl.FileServiceImpl;
import com.yc.cloudvideo.sp_store.service.impl.StoreServiceImpl;
import com.yc.cloudvideo.util.Utils;
import com.yc.common.bean.Result;
import com.yc.common.biz.BizException;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
@Controller
@RequestMapping("/cloudvideo/file")
public class FileController {

    @Resource
    StoreServiceImpl storeService;
    @Resource
    StoreMapper storeMapper;
    @Resource
    StoreFileMapper storeFileMapper;
    @Resource
    FileServiceImpl fileService;
    @Resource
    FileMapper fileMapper;

    @PostMapping("selectFileByPath")
    @ResponseBody
    com.yc.cloudvideo.entity.File selectFileByPath(String path){
        com.yc.cloudvideo.entity.File file = fileMapper.selectFileByPath(path);
        return file;
    }

    /**
     * 文件上传
     * @param cid 仓库id
     * @param savePath  web保存路径
     * @param file  上传的文件
     * @return
     */
    @PostMapping("uploadO")
    @ResponseBody
    Result uploadO(
            @RequestParam("uid")int uid,@RequestParam("cid") int cid,@RequestParam(value = "savePath") String savePath,@RequestBody MultipartFile file) throws BizException, IOException {

        return fileService.upload(uid,cid, savePath, file);
    }
    //传入仓库id，获取五条记录，分页
    @PostMapping("selectFilesByCidPage")
    @ResponseBody
    List<StoreFile> selectFilesByCidPage(int cid,int begin){
        return storeFileMapper.selectFilesByCidPage(cid,begin,5);
    }
    //传入仓库id，获取仓库总记录数
    @PostMapping("selectFilesSumByCid")
    @ResponseBody
    long selectFilesSumByCid(int cid){
        QueryWrapper<StoreFile> qw=new QueryWrapper<>();
        qw.eq("cid",cid);
        return storeFileMapper.selectCount(qw);
    }

    //传入路径和要新建的文件夹名字，进行创建文件夹操作
    @PostMapping("newFolder")
    @ResponseBody
    Result newFolder(int cid,String path, String folderName,
                     int uid
    ){

        try {
            System.out.println("path");
            return storeService.newFolder(uid,cid,path,folderName);
        } catch (BizException e) {
            e.printStackTrace();
            return new Result(-1,e.getMessage(),null);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(-1,e.getMessage(),null);
        }
    }

    //传入仓库id和仓库下的一个路径，获取该路径下的所有内容
    @PostMapping("getFiles")
    @ResponseBody
    List<StoreFile> getFiles(@RequestParam("cid") int cid,@RequestParam("filePath") String filePath){
        //获取到该路径下的 所有 文件信息
        List<StoreFile> storeFiles = storeService.selectFiles(cid,filePath);
        return storeFiles;
    }
    @PostMapping("getStoreNameByCid")
    @ResponseBody
    String getStoreNameByCid(@RequestParam("cid") int cid){
        return storeMapper.selectNameByCid(cid);
    }

    @PostMapping("getStoreByCid")
    @ResponseBody
    Store getStoreByCid(@RequestParam("cid") int cid){
        return storeMapper.selectById(cid);
    }

/**------------------------------------------分割线，下面的代码未使用------------------------------------------------------*/

    //通过一个路径来获取该路径下的资源  Map中存  资源名,文件类型 (目录，视频，图片，其它) 四种
    List<Map<String,String>> getFilesByPath(String path){
        List<Map<String,String>> fileList=new ArrayList<>();
        String truePath = Utils.toTruePath(path);

        File file=new File(truePath);
        //获取该文件夹当前目录下的文件夹和文件
        //判断是文件，则返回空
        if(file.isFile()){
            return null;
        }
        String[] list = file.list();
        for (int i = 0; i < list.length; i++) {
            Map<String,String> map=new HashMap<>();
            File file1=new File(truePath+"\\"+list[i]);
            //如果是目录
            if(file1.isDirectory()){
                map.put(list[i],"目录");
            }else{//如果是文件，则判断文件类型

            }


            fileList.add(map);
        }

        return null;
    }
    //传入文件名，返回文件类型
    //判断文件的具体类型是什么
    String fileType(String fileName){
        String 是视频[] = {".mp4",".wmv"};
        String 是图片[] = {".png",".jpeg",".jpg"};
        //是视频
        for (int i = 0; i < 是视频.length; i++) {
            if (fileName.endsWith(是视频[i])){
                return "视频";
            }
        }
        //图片
        for (int i = 0; i < 是图片.length; i++) {
            if (fileName.endsWith(是图片[i])){
                return "图片";
            }
        }
        //其它
        return  "其它";
    }


}

